package com.testing.login;

import com.alibaba.fastjson.JSONObject;
import com.testing.module.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Classname ${NAME}
 * @Description 类型说明
 * @Date 2021/9/11 22:20
 * @Created by 特斯汀Roy
 */
@WebServlet(name = "Logout")
public class Logout extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/plain;charset=utf-8");
        Result result=new Result();
        if(request.getSession().getAttribute("username")!=null) {
            String user=request.getSession().getAttribute("username").toString();
            result.setMsg("将用户"+user+"注销成功");
            result.setStatusCode("0");
            //让session失效
            request.getSession().invalidate();
        }
        else{
            result.setMsg("虽然没有用户登录，但是也清理一下session");
            result.setStatusCode("1");
            request.getSession().invalidate();
        }
        String s = JSONObject.toJSONString(result);
        System.out.println(s);
        response.getWriter().append(s);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
